package com.example.zjx8969.myapplication

import kotlinx.coroutines.*
import java.util.concurrent.Executors


fun main(): Unit = runBlocking {
    val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
    GlobalScope.launch {
        for (m in 0..5) {
            launch(Dispatchers.Default) {
                for (i in 0..1000) {
                    launch {
                        val instance = ActivityResultFragment.getInstance("thread:$m,arrayIndex:$i")
                        launch(dispatcher) {
                            instance.print()
                        }

                    }

                }
            }

        }
    }.join()
}

class Intent(val value: String)

class ActivityResultFragment private constructor() {
    lateinit var intent: Intent
    var index: Int = -1
    fun print() {
        println("intent=${intent.value}, globalIndex=$index, thread:${Thread.currentThread().name}")
    }

    companion object {
        @JvmStatic
        private var id: Int = 0
            get() {
                field += 1
                if (field > 65535) field = 0
                return field
            }

        //        @Synchronized
        @JvmStatic
        fun getInstance(string: String): ActivityResultFragment {
            return ActivityResultFragment().apply {
                intent = Intent(string)
                index = id
            }
        }
    }
}